package org.inkice.jqt.collectionQueryShell;

import org.inkice.jqt.queryShell.DataQuerySubShell;
import org.inkice.jqt.queryUtil.Filter;

import java.util.*;

public class ArrayListQuerySubShell  implements DataQuerySubShell<HashMap<String,String>> {

    private ArrayList<HashMap<String,String>> dataList;

    public ArrayListQuerySubShell(){
        dataList = new ArrayList<>();
    }

    /**
     * 复制一份数据表，不影响原数据集
     * @param dataList 数据集
     */
    public ArrayListQuerySubShell(ArrayList<HashMap<String,String>> dataList) {
        this.dataList = new ArrayList<>(dataList);
    }


    @Override
    public ArrayListQuerySubShell filter(String key, Filter filter, Object value) {
        dataList.removeIf(record -> !filter.filter(record.get(key),value));
        return this;
    }

    @Override
    public void distinct() {
        HashSet<HashMap<String,String>> set = new HashSet<>(dataList);
        dataList = new ArrayList<>(set);
    }

    public ArrayList<HashMap<String,String>> result(){
        return new ArrayList<HashMap<String, String>>(dataList);
    }
}
